<template>
    <div class="add_apply">
      <head-top head-title="跑楼申请" go-back="true"></head-top>
      <div class="add_main">
        <div class="choice_village">
          <div class="choice_left" @click="choiceVillage">
            <span>选择小区：</span>
            <input type="text" readonly placeholder="请选择小区" v-model="villgeWord">
          </div>
          <img src="../home/images/fanhui1@2x.png" alt="">
        </div>
        <div class="choice_village">
          <div class="choice_left" @click="openName">
            <span>选择项目经理：</span>
            <input type="text" readonly placeholder="请选择项目经理" v-model="name">
          </div>
          <img src="../home/images/fanhui1@2x.png" alt="">
        </div>
        <!--小区弹窗-->
        <mt-popup
          v-model="villageShow"
          popup-transition="popup-fade" class="villageHeights">
          <div class="village">
            <p class="p border-bottom">请选择小区</p>
            <div class="plot-wrapper">
              <div class="search">
                <div class="search-wrapper">
                  <input type="text" placeholder="请输入小区的名称" v-model="keyword" @keyup.enter="search" @input="search">
                  <span class="search-icon" @click="search"></span>
                </div>
              </div>
            </div>
          </div>
          <mt-index-list>
            <mt-index-section :index="index" v-for="(item,index) in data" :key="index">
              <div v-for="(list,index) in item" @click="goVillage(list)">
                <mt-cell :title="list.title"></mt-cell>
              </div>
            </mt-index-section>
          </mt-index-list>
        </mt-popup>
        <!--项目经理弹窗-->
        <mt-popup
          v-model="nameShow"
          position="bottom"
          popup-transition="popup-fade" class="nameHeights">
          <!--<div class="name_popup">
            <div class="name_li" @click="chioceName(name)" v-for="(name, nindex) in nameArr" :key="nindex">{{name.name}}</div>
          </div>-->
          <div class="time_picker">
            <mt-picker :slots="dateSlots" ref="picker1" :show-toolbar="true" value-key="name">
              <span @click="handleCache()" class="btn btn_cancel">取消</span>
              <span @click="handleConfirm()" class="btn btn_sure">确定</span>
            </mt-picker>
          </div>
        </mt-popup>
        <div class="apply_reason">
          <p>申请理由：</p>
          <textarea name="" id="" cols="30" v-model="reason" rows="10"></textarea>
        </div>
        <div class="get_div">
          <button :class="[dis === true ? 'disabled':'']" @click="keepData()"><span>提交</span></button>
        </div>
      </div>
    </div>
</template>

<script>
  import headTop from '../header/Header'
  import {Toast, Indicator} from 'mint-ui'
  import {getVillageLoad, searchKeyword} from '../../api/cityVillage'
  import {addApply, projectManager} from '../../api/my'

  export default {
    name: 'AddApply',
    data () {
      return {
        villageShow: false, // 小区弹窗
        nameShow: false, // 项目经理弹窗
        data: {}, // 获取小区列表数据
        keyword: '', // 搜索值
        nameArr: [], // 项目经理
        name: '', // 项目经理
        nameId: '', // 经理id
        villgeWord: '', // 小区名
        villgeId: '', // 小区id
        reason: '', // 申请理由
        dis: false // 防止按钮重复点击（false: 可点击  true：不可点击）
      }
    },
    created() {
    },
    computed: {
      dateSlots () {
        this.data1 = this.nameArr
        let slots = [
          {
            flex: 1,
            values: this.data1,
            className: 'slot1',
            textAlign: 'center'
          }
        ]
        return slots
      }
    },
    methods: {
      // 小区弹窗
      choiceVillage() {
        this.villageShow = true
        this._getVillage()
      },
      // 获取小区列表
      _getVillage () {
        getVillageLoad().then(res => {
          if (res.code === 200) {
            this.data = res.data
          }
        })
      },
      search () {
        searchKeyword(this.keyword).then(res => {
          if (res.code === 200) {
            this.data = res.data
          }
        })
      },
      goVillage (item) {
        this.villgeWord = item.title
        this.villgeId = item.id
        // console.log(this.villgeId)
        this.keyword = ''
        this.villageShow = false
      },
      // 项目经理弹窗
      openName() {
        this.nameShow = true
        this._projectManager()
      },
      // 获取项目经理
      _projectManager() {
        projectManager().then(res => {
          // console.log(res)
          if (res.code === 200) {
            this.nameArr = res.data
          }
        })
      },
      // picker取消
      handleCache() {
        this.nameShow = false
      },
      // picker确认选择
      handleConfirm () {
        var object1 = this.$refs.picker1.getValues()[0]
        // console.log(object1.name + '--' + object1.project_id)
        this.name = object1.name // 项目经理
        this.nameId = object1.project_id // 经理id
        this.nameShow = false
      },
      chioceName(item) {
        this.name = item.name // 项目经理
        this.nameId = item.project_id // 经理id
        this.nameShow = false
      },
      // 选择项目经理
      keepData() {
        if (this.dis === false) {
          if (!this.villgeId) {
            Toast({
              message: '请选择小区！',
              position: 'middle',
              duration: 2000
            })
            return
          }
          if (!this.nameId) {
            Toast({
              message: '请选择对应小区的项目经理！',
              position: 'middle',
              duration: 3000
            })
            return
          }
          if (!this.reason) {
            Toast({
              message: '请输入申请理由！',
              position: 'middle',
              duration: 2000
            })
            return
          }
          this.dis = true
          Indicator.open({
            text: '..提交中..',
            spinnerType: 'fading-circle'
          })
          addApply(this.villgeId, this.reason, this.nameId).then(res => {
            // console.log(res)
            if (res.code === 200) {
              Toast({
                message: res.data,
                position: 'middle',
                duration: 2000
              })
              Indicator.close()
              this.$router.push('/ApplyRecord')
            }
            if (res.code === 400) {
              this.dis === false
              Toast({
                message: res.error,
                position: 'middle',
                duration: 2000
              })
              Indicator.close()
            }
          })
        }
      }
    },
    components: {
      headTop
    }
  }
</script>

<style lang="scss" scoped>
  @import "../../common/sass/mixin.scss";
.add_apply{
  padding-top: 50px;
  .add_main{
    padding: 10px;
    .choice_village{
      width: 100%;
      display: inline-flex;
      align-items: center;
      justify-content: space-between;
      .choice_left{
        display: inline-flex;
        align-items: center;
        span{
          font-size: 15px;
          color: #333333;
        }
        input{
          background: #ffffff;
          border: none;
          outline: none;
          font-size: 15px;
          padding: 10px 0;
          color: #999999;
        }
      }
      img{
        width: 8px;
        height: 15px;
        transform: rotate(90deg);
      }
    }
    .select_li{
      width: 100%;
      display: inline-flex;
      align-items: center;
      margin: 10px 0;
      span{
        font-size: 15px;
        color: #333333;
      }
      select{
        width: 120px;
        outline: none;
        padding: 5px 0;
        border: 1px solid #999999;
        color: #999999;
        option{
          font-size: 15px;
        }
      }
    }
    .apply_reason{
      margin-top: 10px;
      p{
        font-size: 15px;
        color: #333333;
      }
      textarea{
        width: 94%;
        outline: none;
        border: none;
        background: #F2F2F2;
        border-radius: 5px;
        margin-top: 10px;
        resize: none;
        padding: 10px;
        font-size: 14px;
        height: 100px;
        color: #999999;
      }
    }
    .get_div{
      padding: 10px 0;
      button{
        background: #0078de;
        outline: none;
        width: 100%;
        span {
          line-height: 44px;
          font-size: 15px;
          color: #fff;
        }
      }
      .disabled{
        background: #999999;
      }
    }
  }
  // 弹窗样式(小区)
  .villageHeights {
    width: 100%;
    border-radius: 5px;
    .village {
      .p {
        font-size: 15px;
        text-align: center;
        line-height: 39px;
      }
      .plot-wrapper {
        width: 100%;
        padding-top: 40px;
        background: #fff;
        .search {
          width: 100%;
          position: fixed;
          top: 40px;
          left: 0;
          background-color: #fff;
          text-align: center;
          height: 44px;
          line-height: 44px;
          .now-plot {
            padding-left: 10px;
            text-align: left;
            font-size: 15px;
            height: 40px;
            line-height: 40px;
            background-color: #D9D9D9;
          }
          .search-wrapper {
            display: inline-block;
            height: 30px;
            line-height: 30px;
            width: 80%;
            margin: 0 10px;
            border-radius: 4px;
            background-color: #e6e6e6;
            input {
              width: 118px;
              font-size: 14px;
              outline: none;
              border: none;
              background-color: #e6e6e6;
              &::-ms-input-placeholder {
                text-align: center;
              }
              &::-webkit-input-placeholder {
                text-align: center;
              }
            }
            .search-icon {
              display: inline-block;
              width: 17px;
              height: 17px;
              background-size: 17px 17px;
              background-repeat: no-repeat;
              @include bgimg('../../components/home/images/search');
              vertical-align: middle;
            }
          }
        }
      }
    }
    height: 90%;
    .mint-indexlist {
      height: 80%;
    }
    .mint-indexlist-content {
      height: 100% !important;
      -webkit-overflow-scrolling: touch;
      overflow-scrolling: touch;
    }
    .mint-cell-wrapper {
      background-size: 120% 0px;
    }
  }
  .nameHeights{
    /*width: 70%;
    height: 300px;
    border-radius: 5px;
    overflow-y: scroll;
    .name_popup{
      .name_li{
        border-bottom: 1px solid #e6e6e6;
        padding: 15px 0;
        text-align: center;
        font-size: 15px;
        color: #666666;
      }
    }*/
    width: 100%;
    .time_picker{
      .btn{
        font-size: 16px;
        height: 40px;
        line-height: 40px;
        padding: 0 20px;
        display: inline-block;
      }
      .btn_cancel{
        color: #666666;
      }
      .btn_sure{
        color: #0C7AD9;
        float: right;
      }
    }
  }
}
</style>
